// Copyright (C) Kumo inc. and its affiliates.
// Author: Jeff.li lijippy@163.com
// All rights reserved.
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU Affero General Public License as published
// by the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU Affero General Public License for more details.
//
// You should have received a copy of the GNU Affero General Public License
// along with this program.  If not, see <https://www.gnu.org/licenses/>.
//


#pragma once

#include <pollux/common/file/file_systems.h>

namespace Aws::Auth {
// Forward-declare the AWSCredentialsProvider class from the AWS SDK.
class AWSCredentialsProvider;
} // namespace Aws::Auth

namespace kumo::pollux::filesystems {

bool initializeS3(
    std::string_view logLevel = "FATAL",
    std::optional<std::string_view> logLocation = std::nullopt);

void finalizeS3();

class S3Config;

using AWSCredentialsProviderFactory =
    std::function<std::shared_ptr<Aws::Auth::AWSCredentialsProvider>(
        const S3Config& config)>;

void registerCredentialsProvider(
    const std::string& providerName,
    const AWSCredentialsProviderFactory& factory);

/// Implementation of S3 filesystem and file interface.
/// We provide a registration method for read and write files so the appropriate
/// type of file can be constructed based on a filename.
class S3FileSystem : public FileSystem {
 public:
  S3FileSystem(
      std::string_view bucketName,
      const std::shared_ptr<const config::ConfigBase> config);

  std::string name() const override;

  std::unique_ptr<ReadFile> openFileForRead(
      std::string_view s3Path,
      const FileOptions& options = {}) override;

  std::unique_ptr<WriteFile> openFileForWrite(
      std::string_view s3Path,
      const FileOptions& options) override;

  void remove(std::string_view path) override {
    POLLUX_UNSUPPORTED("remove for S3 not implemented");
  }

  void rename(
      std::string_view path,
      std::string_view newPath,
      bool overWrite = false) override {
    POLLUX_UNSUPPORTED("rename for S3 not implemented");
  }

  bool exists(std::string_view path) override {
    POLLUX_UNSUPPORTED("exists for S3 not implemented");
  }

  std::vector<std::string> list(std::string_view path) override {
    POLLUX_UNSUPPORTED("list for S3 not implemented");
  }

  void mkdir(std::string_view path, const DirectoryOptions& options = {})
      override {
    POLLUX_UNSUPPORTED("mkdir for S3 not implemented");
  }

  void rmdir(std::string_view path) override {
    POLLUX_UNSUPPORTED("rmdir for S3 not implemented");
  }

  std::string getLogLevelName() const;

  std::string getLogPrefix() const;

 protected:
  class Impl;
  std::shared_ptr<Impl> impl_;
};

} // namespace kumo::pollux::filesystems
